package com.zycfc.zsf.boot.job;

import org.springframework.boot.*;
import com.zycfc.zsf.boot.job.autoconfigure.*;
import org.springframework.core.env.*;
import com.zycfc.zsf.boot.job.core.*;
import com.zycfc.zsf.boot.core.common.*;
import com.zycfc.zsf.boot.job.service.*;
import com.dangdang.ddframe.job.reg.zookeeper.*;
import org.springframework.context.*;
import org.springframework.beans.factory.support.*;
import org.springframework.beans.factory.config.*;
import org.slf4j.*;

public class ZsfJobStartupRunner implements CommandLineRunner
{
    private static final Logger log;
    private ZookeeperProperties zookeeperProperties;
    private Environment env;
    
    public ZsfJobStartupRunner(final ZookeeperProperties zookeeperProperties, final Environment env) {
        this.zookeeperProperties = zookeeperProperties;
        this.env = env;
    }
    
    public void run(final String... args) throws Exception {
        try {
            this.initZookeeperRegistryCenter();
            this.initZsfJobSwitch();
            this.initZsfJobRestart();
            this.initZsfJobShardCacheManager();
            this.initZsfJobBeanProcessor();
            this.initZsfJobHandler();
            ZsfJobStartupRunner.log.info("^_^ ZsfJob: \u521d\u59cb\u5316\u52a0\u8f7d\u5b8c\u6210");
        }
        catch (Exception e) {
            ZsfJobStartupRunner.log.error("ZsfJob: \u52a0\u8f7d\u5931\u8d25\uff01", (Throwable)e);
            throw new Exception(e);
        }
    }
    
    private ZsfJobHandler initZsfJobHandler() {
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZsfJobHandler.class);
        beanDefinitionBuilder.addConstructorArgReference("zookeeperRegistryCenter");
        beanDefinitionBuilder.addDependsOn("zookeeperRegistryCenter");
        this.registerBean("zsfJobHandler", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zsfJobHandler initialized ");
        return (ZsfJobHandler)SpringContextHelper.getBean("zsfJobHandler", (Class)ZsfJobHandler.class);
    }
    
    private ZsfJobBeanProcessor initZsfJobBeanProcessor() {
        final String applicationName = this.env.getProperty("spring.application.name");
        if (EnvBaseConstants.getInstance() == null) {
            EnvBaseConstants.build(this.zookeeperProperties.getNamespace(), applicationName);
        }
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZsfJobBeanProcessor.class);
        beanDefinitionBuilder.addConstructorArgValue((Object)this.zookeeperProperties.getNamespace()).addConstructorArgReference("zookeeperRegistryCenter").addConstructorArgValue((Object)this.env);
        beanDefinitionBuilder.addDependsOn("zookeeperRegistryCenter");
        beanDefinitionBuilder.setInitMethodName("init");
        this.registerBean("zsfJobBeanProcessor", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zsfJobBeanProcessor initialized");
        return (ZsfJobBeanProcessor)SpringContextHelper.getBean("zsfJobBeanProcessor", (Class)ZsfJobBeanProcessor.class);
    }
    
    private ZsfJobShardCacheManager initZsfJobShardCacheManager() {
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZsfJobShardCacheManager.class);
        beanDefinitionBuilder.addDependsOn("zookeeperRegistryCenter");
        beanDefinitionBuilder.setInitMethodName("init");
        beanDefinitionBuilder.setDestroyMethodName("close");
        this.registerBean("zsfJobShardCacheManager", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zsfJobShardCacheManager initialized");
        return (ZsfJobShardCacheManager)SpringContextHelper.getBean("zsfJobShardCacheManager", (Class)ZsfJobShardCacheManager.class);
    }
    
    private ZsfJobSwitch initZsfJobSwitch() {
        final String applicationName = this.env.getProperty("spring.application.name");
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZsfJobSwitch.class);
        beanDefinitionBuilder.addConstructorArgReference("zookeeperRegistryCenter").addConstructorArgValue((Object)applicationName);
        beanDefinitionBuilder.addDependsOn("zookeeperRegistryCenter");
        beanDefinitionBuilder.setInitMethodName("init");
        this.registerBean("zsfJobSwitch", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zsfJobSwitch initialized");
        return (ZsfJobSwitch)SpringContextHelper.getBean("zsfJobSwitch", (Class)ZsfJobSwitch.class);
    }
    
    private ZsfJobRestart initZsfJobRestart() {
        final String applicationName = this.env.getProperty("spring.application.name");
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZsfJobRestart.class);
        beanDefinitionBuilder.addConstructorArgReference("zookeeperRegistryCenter").addConstructorArgValue((Object)applicationName);
        beanDefinitionBuilder.addDependsOn("zookeeperRegistryCenter");
        beanDefinitionBuilder.setInitMethodName("init");
        this.registerBean("zsfJobRestart", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zsfJobRestart initialized");
        return (ZsfJobRestart)SpringContextHelper.getBean("zsfJobRestart", (Class)ZsfJobRestart.class);
    }
    
    private void initZookeeperRegistryCenter() {
        this.registryZookeeperConfiguration();
        this.registryZookeeperRegistryCenter();
    }
    
    private ZookeeperRegistryCenter registryZookeeperRegistryCenter() {
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZookeeperRegistryCenter.class);
        beanDefinitionBuilder.addConstructorArgReference("zookeeperConfiguration");
        beanDefinitionBuilder.addDependsOn("zookeeperConfiguration");
        beanDefinitionBuilder.setInitMethodName("init");
        beanDefinitionBuilder.setDestroyMethodName("close");
        this.registerBean("zookeeperRegistryCenter", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zookeeperRegistryCenter initialized");
        return (ZookeeperRegistryCenter)SpringContextHelper.getBean("zookeeperRegistryCenter", (Class)ZookeeperRegistryCenter.class);
    }
    
    private ZookeeperConfiguration registryZookeeperConfiguration() {
        final BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition((Class)ZookeeperConfiguration.class);
        beanDefinitionBuilder.addConstructorArgValue((Object)this.zookeeperProperties.getServers()).addConstructorArgValue((Object)this.zookeeperProperties.getNamespace());
        if (this.zookeeperProperties.getDigest() != null && this.zookeeperProperties.getDigest().length() > 0) {
            beanDefinitionBuilder.addPropertyValue("digest", (Object)this.zookeeperProperties.getDigest());
        }
        beanDefinitionBuilder.addPropertyValue("maxRetries", (Object)this.zookeeperProperties.getMaxRetries());
        beanDefinitionBuilder.addPropertyValue("sessionTimeoutMilliseconds", (Object)this.zookeeperProperties.getSessionTimeoutMilliseconds());
        beanDefinitionBuilder.addPropertyValue("baseSleepTimeMilliseconds", (Object)this.zookeeperProperties.getBaseSleepTimeMilliseconds());
        beanDefinitionBuilder.addPropertyValue("maxSleepTimeMilliseconds", (Object)this.zookeeperProperties.getMaxSleepTimeMilliseconds());
        beanDefinitionBuilder.addPropertyValue("connectionTimeoutMilliseconds", (Object)this.zookeeperProperties.getConnectionTimeoutMilliseconds());
        beanDefinitionBuilder.addDependsOn("springContextHelper");
        this.registerBean("zookeeperConfiguration", beanDefinitionBuilder);
        ZsfJobStartupRunner.log.info("ZsfJob: zookeeperConfiguration initialized");
        return (ZookeeperConfiguration)SpringContextHelper.getBean("zookeeperConfiguration", (Class)ZookeeperConfiguration.class);
    }
    
    private void registerBean(final String beanName, final BeanDefinitionBuilder beanDefinitionBuilder) {
        final ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext)SpringContextHelper.getApplicationContext();
        final DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory)configurableApplicationContext.getBeanFactory();
        defaultListableBeanFactory.registerBeanDefinition(beanName, (BeanDefinition)beanDefinitionBuilder.getRawBeanDefinition());
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZsfJobStartupRunner.class);
    }
}
